<?php
class omeauto_autobind{
    
    function get_conditions(){
        return kernel::single('omeauto_auto')->get_conditions('autobind');
    }
    
    function autobind($delivery_sdf){
        $filter = array();
        
        $oAutobind = &app::get('omeauto')->model('autobind');
        $oOrder = &app::get('ome')->model('orders');
        
        ////////判断发货单对应订单是否为销售单，如果是那么返回FALSE不与合并//////
        $doObj = &app::get('ome')->model('delivery_order');        
        $do = $doObj->dump(array('delivery_id'=>$delivery_sdf['delivery_id']));
        $o = $oOrder->dump($do['order_id'],'order_type');
        if ($o['order_type'] == 'sale' || $o['is_fail'] == 'true') return false;
        //////////////////////////////////////////////////////////////////////
        
        $rows = $oAutobind->getList("*",array(),0,1);
        if($rows){
            $row = $rows[0];
            $flag = false;
            $config = $row['config'];
            if($config){
                foreach($config as $condition=>$value){
                    $oCondition = kernel::single($condition);
                    foreach($value as $v){
                        $flag = $oCondition->validate($delivery_sdf,$v);
                        $filter[] = $oCondition->filter($v);
                        if(!$flag){
                            $filter = array();
                            break;
                        }
                    }
                    if(!$flag){
                        break;
                    }
                }
            }
            if($flag){
                $where = implode(" AND ",$filter);
                $where = $where ? $where : 1;
                $db = kernel::database();
                $delivery_list = $db->select("SELECT delivery_id FROM sdb_ome_delivery WHERE parent_id=0 AND type='normal' AND process='false' AND status IN('ready','progress') AND bind_key='".$delivery_sdf['bind_key']."' AND logi_id=".$delivery_sdf['logi_id']." AND ".$where);
                
                if($delivery_list){
                    $ids = array();
                    foreach($delivery_list as $v){
                        $ids[] = $v['delivery_id'];
                    }
                    
                    if(!in_array($delivery_sdf['delivery_id'],$ids)){
                        $ids[] = $delivery_sdf['delivery_id'];
                    }
                    
                    if(count($ids) > 1){
                        $concurrent = &app::get('ome')->model('concurrent');
                        $time = time();
                        foreach($ids as $id){
                            $crc = md5($id.$time);
                            if(!$concurrent->is_pass($crc,'delverycrc')){
                                return false;
                            }
                        }
                        $oDelivery = app::get('ome')->model('delivery');
                        $oDelivery->merge($ids);
                    }else{
                        return false;
                    }
                }
            }
        }
    }
}